package com.icomvn.game.pikachu.object;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.view.KeyEvent;

import com.icomvn.game.pikachu.R;

public class AlertObject {
	public AlertObject() {
	}

	static boolean finish = false;
	static Activity activity = null;

	public static void alert(String text, String message, Activity activity,
			boolean finish, boolean isCancel) {
		final Boolean isCancel1 = isCancel;
		AlertObject.finish = finish;
		AlertObject.activity = activity;
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setTitle(text);
		builder.setMessage(message);
		OKListener pl1 = new OKListener();
		CancelListener pl2 = new CancelListener();
		// builder.setMessage("");
		builder.setPositiveButton("Đồng ý", pl1);
		if (isCancel)
			builder.setNegativeButton("Hủy bỏ", pl2);
		// Create the dialog
		AlertDialog ad = builder.create();
		ad.setOnKeyListener(new OnKeyListener() {

			public boolean onKey(DialogInterface arg0, int keyCode,
					KeyEvent arg2) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					if (AlertObject.finish && (!isCancel1))
						AlertObject.activity.finish();
					AlertObject.activity.overridePendingTransition(
							R.anim.diagslide_enter_back,
							R.anim.diagslide_leave_back);
				}
				return false;
			}
		});
		// show
		ad.show();
		ad.getButton(AlertDialog.BUTTON_POSITIVE).setDrawingCacheEnabled(true);
		ad.getButton(AlertDialog.BUTTON_NEGATIVE).setDrawingCacheEnabled(true);
	}

	public static class OKListener implements
			android.content.DialogInterface.OnClickListener {
		public void onClick(DialogInterface v, int buttonId) {
			if (AlertObject.finish) {
				AlertObject.activity.finish();
				AlertObject.activity.overridePendingTransition(
						R.anim.diagslide_enter_back,
						R.anim.diagslide_leave_back);
			}
		}
	}

	public static class CancelListener implements
			android.content.DialogInterface.OnClickListener {
		public void onClick(DialogInterface v, int buttonId) {
		}
	}

}
